<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hypref_mall
 * contact  hyperf_mall@163.com
 */

declare(strict_types=1);

namespace App\Repository;

use App\Model\MemberFooterModel;
use Hyperf\Di\Annotation\Inject;

class MemberFooterRepository extends BaseRepository
{
    /**
     * @Inject
     * @var MemberFooterModel
     */
    protected $model;

    /**
     * Notes: 重写添加方法
     * Author: Bruce.z
     * DateTime: 2022/10/24 10:25
     * @param $data
     * @return bool
     */
    public function baseAdd($data): bool
    {
        $data['year']=date("Y");
        $data['month']=date("m");
        $data['day']=date("d");

        $re = $this->getOne($data,['id']);
        if(!empty($re)){
            $this->updateByConditions(['id'=>$re['id']],['create_time'=>time()]);
        }else{
            parent::baseAdd($data);
        }

        return true;
    }

    /**
     * Notes: 根据条件删除全部
     * Author: Bruce.z
     * DateTime: 2022/10/24 11:25
     * @param array $condition
     * @return int|mixed
     */
    public function removeAll(array $condition)
    {
        return $this->_searchFactor($condition)->delete();
    }

    public function getPageRows(array $conditions = [], array $fields = ['*'], int $page = 1, int $size = 10): array
    {
        $count = $this->_searchFactor($conditions)->count();
        $offset = max(0, ($page-1)*$size);
        $data = $this->_searchFactor($conditions)->groupBy("goods_id")->orderByDesc($this->model->getPrimaryKey())->offset($offset)->limit($size)->get($fields)->toArray();

        return ['total'=>$count, 'data'=>$data];
    }
}
